// pages/shopxiangqing/shopxiangqing.js
const app = getApp()
const api = require("../../utils/api.js")
console.log(api)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: null,
    imgs:[]
    
  }, 
  previewImage: function (e) {
    var that =this;
    console.log(e)
    var imgs=[];
    var current = e.target.dataset.src.img_url;
    for (var i = 0;i<that.data.gallery.length;i++)
    {
      imgs.push(that.data.gallery[i].img_url);
    }
    that.setData({
      imgs
    })
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: that.data.imgs // 需要预览的图片http链接列表  
    })
  },
  toCar() {
    wx.switchTab({
      url: '/pages/shop/shop'
    })
  },
  // 立即购买
  immeBuy(event) {
    var that =this;
    wx.showToast({
      title: '购买成功',
      icon: 'success',
      duration: 2000
    });
    console.log(event)
    var goodsId = event.currentTarget.dataset.add[0].goods_id;
    var goods_number = event.currentTarget.dataset.add[0].goods_number;
    var goods_sn = event.currentTarget.dataset.add[0].id;
    console.log(goodsId)
    console.log(goods_number)
    console.log(goods_sn)
    let token = app.globalData.token
    if (token == undefined) {
      wx.showToast({
        title: '您没有登录',
        icon: 'success',
        duration: 20000
      });
    }
    wx.request({
      
      url: api.addshop,
      method:'POST',
      
      data: {
        goodsId: goodsId,
        number: 1,
        productId: goods_sn
      },
      header: {
        'X-Nideshop-Token': token
      },
      success(addres){
        console.log(addres)
      }
    })
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var goodsId = options.id
    this.setData({
      goodsId,
      attribute: [], //广告位数据
      comment: [], //频道数据
      gallery: [],
      info: [],
      productList:[]

    })
  },

  onShow: function() {
    var that =this;
    wx.request({
      url: api.shopxiangqing,
      data: {
        id: that.data.goodsId
      },
      success(res) {
        console.log("获取的数据", res)
        that.setData({
          attribute: res.data.data.attribute, //广告位数据
          comment: res.data.data.comment, //频道数据
          gallery: res.data.data.gallery, //新品列表
          info: res.data.data.info,
          productList: res.data.data.productList
        })
      },
    })
  }
})